package han.controller;

import han.pojo.Record;
import han.pojo.countRecord;
import han.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @authou HanDianWei
 * @create 2020-10-28 16:11
 */
@Controller
@RequestMapping("/record")
public class RecordController {
    @Autowired
    @Qualifier("RecordService")
    private RecordService recordService;
//    获取全部的记录  游客
    @RequestMapping("/allRecord")
    public String list(Model model){
        List<Record> records = recordService.queryRecord();
        model.addAttribute("list",records);
        return "tourists/allRecord";
    }

    //    获取全部的记录 城管
    @RequestMapping("/allRecordC")
    public String list1(Model model){
        List<Record> records = recordService.queryRecord();
        model.addAttribute("list",records);
        return "admin/allRecordC";
    }


    //    根据id查看案件
@RequestMapping("/recordDetail/{p_id}")
public String selectByPrimaryKey(@PathVariable("p_id")int id, Model model) throws Exception{
    Record record = recordService.selectByPrimaryKey(id);
    List<Record> records = new ArrayList<Record>();
    records.add(record);
    System.out.println(records);
    model.addAttribute("rDetail",records);
    return "tourists/recordDetail";
}

    //    根据案件名称模糊查询
    @RequestMapping("/queryRecordByName")
    public String queryRecordByName(String getRecordByName, Model model){
        List<Record> records = recordService.queryRecordByName(getRecordByName);
        model.addAttribute("list",records);
        return "admin/allRecordC";
    }
    //    根据案件名称模糊查询
    @RequestMapping("/queryRecordByClass")
    public String queryRecordByClass(String getRecordByClass, Model model){
        List<Record> records = recordService.queryRecordByClass(getRecordByClass);
        model.addAttribute("list",records);
        return "admin/allRecordC";
    }

//    跳转到修改
    @RequestMapping("/toUpdate")
    public String toUpdate(int id,Model model){
        Record record = recordService.selectByPrimaryKey(id);
        model.addAttribute("uRecord",record);
        return "admin/updateRecord";
    }

    //    修改
    @RequestMapping("/updateRecord")
    public String update(Record record){
        recordService.updateByPrimaryKey(record);
        return "redirect:/record/allRecordC";
    }

//   删除  不需要跳转新的页面
@RequestMapping("/delRecord")
public String delRecord(int p_id){
        recordService.deleteByPrimaryKey(p_id);
    return "redirect:/record/allRecordC";
}
// 添加
//跳转到添加页面
    @RequestMapping("/toAddRecord")
    public String toAddRecord(){
        return "admin/addRecord";
    }

    @RequestMapping("/addRecord")
    public String addRecord(Record record){
        recordService.insert(record);
        return "redirect:/record/allRecordC";
    }

    //跳转到统计页面
    @RequestMapping("/toChart")
    public String toChart(Model model){
        List<countRecord> recordByWeek = recordService.countRecordByWeek();
        List<countRecord> recordByClass = recordService.countRecordByClass();
        List<countRecord> recordAll = recordService.countRecordAll();
        model.addAttribute("list",recordAll);
        List<String> list = new ArrayList<String>();
        for (int i=0; i<recordByWeek.size(); i++){
            list.add('"'+recordByWeek.get(i).getPtime()+'"');
        }
        model.addAttribute("recordByTime",list);
        List<Integer> list1 = new ArrayList<Integer>();
        for (int i=0; i<recordByWeek.size(); i++){
            list1.add(recordByWeek.get(i).getCountId());
        }
        model.addAttribute("recordByTime1",list1);


        List<Integer> list2 = new ArrayList<Integer>();
        for (int i=0; i<recordByClass.size(); i++){
            list2.add(recordByClass.get(i).getCountId());
        }
        model.addAttribute("recordByClass",list2);
        return "admin/chart";
    }



        @RequestMapping("/byTime")
    public String byTime(@RequestParam("ptime1") String ptime1,@RequestParam("ptime2") String ptime2, Model model){
        List<countRecord> recordByTime = recordService.countRecordByTime(ptime1,ptime2);
        List<countRecord> recordByClass = recordService.countRecordByClass();
        List<String> list4 = new ArrayList<String>();
        for (int i=0; i<recordByTime.size(); i++){
            list4.add('"'+recordByTime.get(i).getPtime()+'"');
        }
        model.addAttribute("recordByTime",list4);
        System.out.println("这里错了》》》》》"+list4);
        List<Integer> list5 = new ArrayList<Integer>();
        for (int i=0; i<recordByTime.size(); i++){
            list5.add(recordByTime.get(i).getCountId());
        }
        model.addAttribute("recordByTime1",list5);

            List<Integer> list6 = new ArrayList<Integer>();
            for (int i=0; i<recordByClass.size(); i++){
                list6.add(recordByClass.get(i).getCountId());
            }
            model.addAttribute("recordByClass",list6);

            List<countRecord> recordAll = recordService.countRecordAll();
            model.addAttribute("list",recordAll);
            return "admin/chart";
    }
}
